package com.ziwei.material.config;

import com.xxl.job.core.executor.impl.XxlJobSpringExecutor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by yangyeze on 2020/6/28.
 */
@ConditionalOnProperty(name="xxl.job.enable", havingValue = "true")
@Slf4j
@Configuration
public class XxlJobConfiguration {
    private final static Integer XXL_JOP_PORT = 9999;
    @Value("${spring.application.name}")
    private String applicationName;
    @Value("${xxl.job.admin.addresses}")
    private String adminAddresses;

    @Value("${spring.profiles.active:NA}")
    private String applicationEnv;


//
//    @Value("${xxl.job.executor.address}")
//    private String address;
//
//    @Value("${xxl.job.executor.ip:127.0.0.1}")
//    private String ip;
//
//    @Value("${xxl.job.executor.port:9999}")
//    private int port;



    @Value("${xxl.job.executor.logretentiondays:30}")
    private int logRetentionDays;


    @Bean
    public XxlJobSpringExecutor xxlJobExecutor() {
        log.info(">>>>>>>>>>> xxl-job config init.");
        XxlJobSpringExecutor xxlJobSpringExecutor = new XxlJobSpringExecutor();
        xxlJobSpringExecutor.setAdminAddresses(adminAddresses);
        String appName = applicationName ;
        log.info(">>>>>>>>>>> xxl-job config init appname " + appName);
        xxlJobSpringExecutor.setAppname(appName);

        xxlJobSpringExecutor.setPort(XXL_JOP_PORT);
        xxlJobSpringExecutor.setLogPath("data/logs/" + applicationEnv + "/" +applicationName +"/job/");
        xxlJobSpringExecutor.setLogRetentionDays(logRetentionDays);
        return xxlJobSpringExecutor;
    }
}
